from rest_framework import status
from django_filters import rest_framework as filters
from common.viewset import CustomViewBase
from common.utils.response import JsonResponse
from accounts.models import (
    Permission,
    Role,
    Department,
    Consumer,
    StorageMenu,
    VideoFileAddres, config,
)
from accounts.serializers import (
    PermissionSerializer,
    RoleSerializer,
    DepartmentSerializer,
    ConsumerSerializer,
    MenuSerializer,
    VideoFileAddresSerializer, configSerializer,
)
from accounts.filters import (
    MenuFilter,
    VideoUrlFilter,
    UserFilter, configFilter,
)


class PermissionViewset(CustomViewBase):
    queryset = Permission.objects.all().order_by("-id")
    serializer_class = PermissionSerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filterset_fields = "__all__"


class RoleViewset(CustomViewBase):
    queryset = Role.objects.all().order_by("-id")
    serializer_class = RoleSerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filterset_fields = "__all__"


class configViewset(CustomViewBase):
    queryset = config.objects.all().order_by("-id")
    serializer_class = configSerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filterset_class = configFilter
    filterset_fields = "__all__"

class DepartmentViewset(CustomViewBase):
    queryset = Department.objects.all().order_by("-id")
    serializer_class = DepartmentSerializer
    filter_backends = (filters.DjangoFilterBackend,)

    filterset_fields = "__all__"


class ConsumerViewset(CustomViewBase):
    queryset = Consumer.objects.all().order_by("-id")
    serializer_class = ConsumerSerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filterset_class = UserFilter

    def destroy(self, request, *args, **kwargs):
        # 进行逻辑删除  将status的状态改成false
        instance = self.get_object()
        serializeries = self.get_serializer(instance)
        data = serializeries.data
        data["is_delete"] = True
        serializer = self.get_serializer(instance, data=data)
        serializer.is_valid(raise_exception=False)
        # self.perform_update(serializer)

        # 真正的删除，彻底的删除掉
        return JsonResponse(
            data=[{"msg": "删除成功"}],
            code=status.HTTP_200_OK,
            msg="删除成功",
            status=status.HTTP_200_OK,
        )


class MenuViewset(CustomViewBase):
    queryset = StorageMenu.objects.filter(parentPath=None, status=True).order_by(
        "order"
    )
    serializer_class = MenuSerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filterset_fields = "__all__"
    filterset_class = MenuFilter

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        # 分页结果
        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)
        # 序列化
        serializer = self.get_serializer(queryset, many=True)
        return JsonResponse(
            data=serializer.data,
            code=status.HTTP_200_OK,
            msg="列表获取成功",
            status=status.HTTP_200_OK,
        )


# 视频上传地址
class VideoUrlViewSet(CustomViewBase):
    queryset = VideoFileAddres.objects.all().order_by("-id")
    serializer_class = VideoFileAddresSerializer
    filter_class = VideoUrlFilter
    filter_backends = (filters.DjangoFilterBackend,)
    filterset_fields = "__all__"